package com.chenhao.springcloud.chenhaomqservice.service;

import com.chenhao.springcloud.chenhaomqservice.config.MQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 *
 * 消费者
 * @author chenhao
 *
 * @date 2020/5/26 16:05
 */


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;

@Component
@Slf4j
public class MQReceiver {

    @RabbitListener(queues = MQConfig.DELAY_QUEUE)
    @RabbitHandler
    public void onDelayMessage(Message msg, Channel channel) throws IOException {
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, true);
        System.out.println("延迟队列在" + LocalDateTime.now()+"时间," + "延迟后收到消息:" + new String(msg.getBody()));
    }

}